package com.venutech.gongan.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.venutech.gongan.service.DevicesQueryAllTaskService;
import com.venutech.gongan.util.DevicesHttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * 定时任务
 */
@Component
@EnableScheduling
@EnableAsync
/*@RestController*/
public class DevicesQueryAllTask {
    private static final Logger logger = LoggerFactory.getLogger(DevicesQueryAllTask.class);

    @Autowired
    DevicesQueryAllTaskService devicesQueryAllTaskService;


    /**
     * 运维接口定时任务
     * @throws Exception
     */
    @Async
    @Scheduled(cron = "0 0 0,1,2 ? * MON-FRI")
   /* @RequestMapping(value = "/api/external/devicesquery/type",method = RequestMethod.GET)
    @ResponseBody*/
    public void getDevicesQueryAllTask() throws Exception {
        try {
            String date_str=String.valueOf(new Date().getTime()/1000-24*3600);
            System.err.println(date_str);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String startTime=sdf.format(new Date(Long.parseLong(String.valueOf(date_str+"000"))));
            String res = DevicesHttpUtil.getGetFaultInfo(startTime);
            JSONArray array  = JSONArray.parseArray(JSONObject.parseObject(res).get("rows").toString());
            if (array.size()>0) {
                devicesQueryAllTaskService.toJavaObjectDeviceSquery(array);
            }
        } catch (Exception e) {
            logger.error("运维接口定时任务获取数据失败,e={}",e.getMessage());
        }
    }

}
